<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class OrderPayment extends BaseModel
{
    protected $attributes = [
        'order_type' => [
            'order' => '普通订单',
            'wholesal' => '经销商订单'
        ],
        'pay_status' => [
            '00' => '审核中',            // 审核中
            '01' => '支付成功',         // 支付成功
            '02' => '支付失败',         // 支付失败

        ],
        // 'return_status' => [
        //     '01' => '退款成功',
        //     '02' => '退款失败',
        // ],
        'payment_method' => [
            // 1 => 'Coinpal Pay',
            // 2 => 'Coinbase Pay',
            3 => 'Wire Transfer',
            10 => '现金',
        ],
        'order_status' => [
            1 => '待审核运费',  // 提交待审核，（订单包含大陆仓发货或者大陆收货地址，需后台人工确认运费）
            10 => '待支付',  // 前端显示
            20 => '审核中', // 前端显示
            30 => '待发货',
            40 => '部分发货',
            50 => '待收货',  // 前端显示
            70 => '已取消',  // 前端显示
            80 => '售后/退款',
            90 => '待评价',
            100 => '已完成'  // 前端显示
        ],
        'pay_type' => [
            '00' => '收款',
            '01' => '退压金',
            '02' => '退款'
        ],
        'status' => [
            '00' => '待审核',
            '01' => '支付成功',
            '02' => '支付失败',
            '03' => '待支付',
            '04' => '驳回'
        ],
        'transfer_type' => [
            1 => 'Blockchain',
            2 => 'Bank Transfer',
            3 => '银行对公',
            4 => '支付宝',
            5 => '微信',
            6 => '第三方代收'
        ],
        'fee_type' => [
            1 => '订单费用',
            2 => '物流费用',
            3 => '增补收入'
        ]

    ];

    public function getSelfAttr()
    {
        return $this->attributes;
    }

    protected $enumCallbacks = [
        'currency' => 'getCurrencyEnum'
    ];

    public function getCurrencyEnum()
    {
        return TransactionCurrency::where('is_del', 0)->column('id AS value,name as label');
    }

    // public function getTransferVoucherUrlAttr($value, $data)
    // {
    //     $oImages = new Images();
    //     $file = $oImages->getImageById($data['transfer_voucher_id']);
    //     return $file['url'];
    // }

    public function getPaymentMethodAttr($value, $data)
    {
        return (int)$value;
    }

    // 保持原始值不变，通过追加字段显示文本
    public function getStatusTextAttr($value, $data)
    {
        return $this->attributes['status'][$data['status']] ?? $data['status'];
    }

    // 或者返回包含原始值和文本的对象
    public function getStatusInfoAttr($value, $data)
    {
        return [
            'value' => $data['status'],
            'text' => $this->attributes['status'][$data['status']] ?? $data['status']
        ];
    }

    public function user()
    {
        return $this->belongsTo(Customer::class, 'user_id', 'id')->bind(['user_name' => 'cusname', 'user_email' => 'email']);
    }

    public function reviewer()
    {
        return $this->belongsTo(Operator::class, 'reviewer', 'id')->bind(['reviewer_name' => 'cname']);
    }

    public function creator()
    {
        return $this->belongsTo(Operator::class, 'creator', 'id')->bind(['creator_name' => 'cname']);
    }

    public function orders()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id')->bind(['total_amount' => 'order_amount', 'order_status' => 'status']);
    }

    public function whoseals()
    {
        return $this->belongsTo(WholesalOrder::class, 'order_id', 'id')->bind(['total_amount', 'order_status' => 'status']);
    }

    public function getFieldEnum($field)
    {
        return $this->getEnum($field, [], 'label');
    }

    public function payCurrency()
    {
        return $this->belongsTo(TransactionCurrency::class, 'pay_currency', 'id')->bind(['name' => 'pay_currency_name']);
    }



    /**
     *
     *   获取属性
     */
    public function getAttributes($field)
    {
        if (isset($this->attributes[$field])) {
            $enum = $this->attributes[$field];
            if (is_array($enum)) {
                return $enum;
            }
        }
    }
}
